//
//
//
#include <irrlicht.h>
#include "CCombatHandler.h"
#include "CGameClient.h"

CCombatHandler*	CCombatHandler::m_pInstance = NULL;

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

CCombatHandler::CCombatHandler()
{

}

CCombatHandler*	CCombatHandler::Instance()
{
	if ( m_pInstance == NULL )
	{
		m_pInstance = new CCombatHandler;
	}

	return	m_pInstance;
}


bool	CCombatHandler::OnEvent(const irr::SEvent& event)
{
	if ( event.EventType == EET_KEY_INPUT_EVENT )
	{
		CGameClient* game = CGameClient::Instance();
		IAnimatedMeshSceneNode*	avatar = NULL;
		vector3df	pos;
		if ( event.KeyInput.Char == 'w' )
		{
			avatar = game->GetAvatars(0);
			pos = avatar->getPosition();
			pos.Z++;
			avatar->setPosition( pos );
			avatar->setFrameLoop(0, 10);
			avatar->setAnimationSpeed( 10 );
			avatar->setLoopMode(false);
			avatar->setRotation( vector3df(0,0,0) );
		}

		if ( event.KeyInput.Char == 's' )
		{
			avatar = game->GetAvatars(0);
			pos = avatar->getPosition();
			pos.Z--;
			avatar->setPosition( pos );
			avatar->setPosition( pos );
			avatar->setFrameLoop(0, 10);
			avatar->setAnimationSpeed( 10 );
			avatar->setLoopMode(false);
			avatar->setRotation( vector3df(0,180,0) );
		}

		if ( event.KeyInput.Char == 'a' )
		{
			avatar = game->GetAvatars(0);
			pos = avatar->getPosition();
			pos.X++;
			avatar->setPosition( pos );
			avatar->setPosition( pos );
			avatar->setFrameLoop(0, 10);
			avatar->setAnimationSpeed( 10 );
			avatar->setLoopMode(false);
			avatar->setRotation( vector3df(0,90,0) );
		}

		if ( event.KeyInput.Char == 'd' )
		{
			avatar = game->GetAvatars(0);
			pos = avatar->getPosition();
			pos.X--;
			avatar->setPosition( pos );
			avatar->setPosition( pos );
			avatar->setFrameLoop(0, 10);
			avatar->setAnimationSpeed( 10 );
			avatar->setLoopMode(false);
			avatar->setRotation( vector3df( 0,270,0 ) );
		}

		if ( event.KeyInput.Char == 'e' )
		{
			avatar = game->GetAvatars(0);
			avatar->setFrameLoop( 30, 40 );
			avatar->setAnimationSpeed(10);
			avatar->setLoopMode(false);
			pos = avatar->getPosition();

			IAnimatedMeshSceneNode* x;
			for ( int i = 1; i < game->GetAvatarNum(); i++ )
			{
				f32	dis = pos.getDistanceFrom( game->GetAvatars(i)->getPosition() );
				if ( dis <= 5 )
				{
					x = game->GetAvatars(i);
					x->setFrameLoop( 155, 170 );
					x->setAnimationSpeed(10);
					x->setLoopMode(false);
				}
			}
		}
	}

	return false;	
}


bool	CCombatHandler::Update()
{
	CGameClient::Instance()->GetNetwork()->Update();

	return true;
}

